﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{
    public partial class FormAcrTool : Form
    {
        HObject m_Image;
        SetArc m_SetArc;
        public FormAcrTool(HObject _Image, SetArc _SetArc)
        {
            try
            {
                InitializeComponent();
                HOperatorSet.GenEmptyObj(out m_Image);
                m_SetArc = _SetArc;
                m_Image = _Image;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void FormAcrTool_Load(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.SetDraw(this.hWindowControl1.HalconWindow, "margin");
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text += "图像为空，请加载图像！";
                }
                else
                {
                    HTuple Width = new HTuple(), Height = new HTuple();
                    HOperatorSet.GetImageSize(m_Image, out Width, out Height);
                    HOperatorSet.SetSystem("width", Width);
                    HOperatorSet.SetSystem("height", Height);
                    MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
                    m_Image.DispObj(hWindowControl1.HalconWindow);
                    tool_ResultInfo.Text = "图像加载成功。";
                }
                if (m_SetArc != null)
                {
                    m_SetArc.ReadParameter();
                    Combox_LineWidth.Text = m_SetArc.LineWidth.ToString();
                    Combox_CrossSize.Text = m_SetArc.CrossSize.ToString();
                    Combox_CrossColor.Text = m_SetArc.CrossColor.ToString();
                    Combox_CalibColor.Text = m_SetArc.CalibColor.ToString();
                    Combox_LinesColor.Text = m_SetArc.ArcColor.ToString();
                    this.propertyGrid1.SelectedObject = m_SetArc;
                    this.propertyGrid1.Refresh();
                    RefreshContour();
                }
                else
                {
                    tool_ResultInfo.Text += "线段工具模块对象不存在。";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }

        private void btn_DrawAcr_Click(object sender, EventArgs e)
        {
            if (txt_AngleStart.Text.Trim() != string.Empty && txt_AngleStart.Text.Trim() != string.Empty)
            {
                btn_DrawAcr.Enabled = false;
                btn_Clear.Enabled = false;
                button2.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                m_Image.DispObj(hWindowControl1.HalconWindow);
                hWindowControl1.Focus();
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "左键绘制，右键结束", "window", 20, 20, 15, "green");
                HOperatorSet.DrawCircle(hWindowControl1.HalconWindow, out HTuple CenterY, out HTuple CenterX, out HTuple Radius);
                double AngleStart = Convert.ToDouble(txt_AngleStart.Text.Trim());
                double AngleEnd = Convert.ToDouble(txt_AngleEnd.Text.Trim()); ;
                //double[] Rows = new double[5];
                //double[] Cols = new double[5];
                double AngleStartRad = MyHOperatorSet.DegToRad(AngleStart);
                double AngleEndRad = MyHOperatorSet.DegToRad(AngleEnd);
                double AngleSpan = (AngleEndRad - AngleStartRad) / 4;
                m_SetArc.Rows[0] = CenterY - Radius * Math.Sin(AngleStartRad);
                m_SetArc.Cols[0] = CenterX + Radius * Math.Cos(AngleStartRad);
                m_SetArc.Rows[4] = CenterY - Radius * Math.Sin(AngleEndRad);
                m_SetArc.Cols[4] = CenterX + Radius * Math.Cos(AngleEndRad);
                m_SetArc.Rows[1] = CenterY - Radius * Math.Sin(AngleStartRad + AngleSpan);
                m_SetArc.Cols[1] = CenterX + Radius * Math.Cos(AngleStartRad + AngleSpan);
                m_SetArc.Rows[2] = CenterY - Radius * Math.Sin(AngleStartRad + AngleSpan * 2);
                m_SetArc.Cols[2] = CenterX + Radius * Math.Cos(AngleStartRad + AngleSpan * 2);
                m_SetArc.Rows[3] = CenterY - Radius * Math.Sin(AngleStartRad + AngleSpan * 3);
                m_SetArc.Cols[3] = CenterX + Radius * Math.Cos(AngleStartRad + AngleSpan * 3);

                HOperatorSet.GenCircle(out HObject circle, CenterY, CenterX, Radius);
                HOperatorSet.DispObj(circle, hWindowControl1.HalconWindow);
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制成功", "window", 20, 20, 15, "green");
                btn_DrawAcr.Enabled = true;
                btn_Clear.Enabled = true;
                button2.Enabled = true;
                btn_Refresh.Enabled = true;
                btn_SaveParameter.Enabled = true;
            }
            else
            {
                MessageBox.Show("圆弧起始角度和终止角度不能为空");
            }

        }

        /// <summary>
        /// 控件刷新
        /// </summary>
        public void RefreshContour()
        {
            if (m_SetArc.DisplayLoction == true && m_SetArc.DisplayArc == true && m_SetArc.DisplayCalib == true)
            {
                checkBo_All.Checked = true;
            }
            else
            {
                if (m_SetArc.DisplayLoction)
                {
                    checkBox_Location.Checked = true;
                }
                else
                {
                    checkBox_Location.Checked = false;
                    m_SetArc.DisplayLoction = false;
                    if (checkBo_All.Checked)
                    {
                        checkBo_All.Checked = false;
                    }
                }

                if (m_SetArc.DisplayArc)
                {
                    checkBox_Lines.Checked = true;
                }
                else
                {
                    checkBox_Lines.Checked = false;
                    m_SetArc.DisplayArc = false;
                    if (checkBo_All.Checked)
                    {
                        checkBo_All.Checked = false;
                    }
                }

                if (m_SetArc.DisplayCalib)
                {
                    checkBox_Caliper.Checked = true;
                }
                else
                {
                    checkBox_Caliper.Checked = false;
                    m_SetArc.DisplayCalib = false;
                    if (checkBo_All.Checked)
                    {
                        checkBo_All.Checked = false;
                    }
                }
            }
        }

        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            MyHOperatorSet.ZoomImage(hWindowControl1, m_Image, e);
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
            HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HTuple Width = new HTuple(), Height = new HTuple();
            HOperatorSet.GetImageSize(m_Image, out Width, out Height);
            MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
            m_Image.DispObj(hWindowControl1.HalconWindow);
            m_SetArc.Cols = new double[5];
            m_SetArc.Rows = new double[5];
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            if (m_SetArc != null)
            {
                m_SetArc.ReadParameter();
                Combox_LineWidth.Text = m_SetArc.LineWidth.ToString();
                Combox_CrossSize.Text = m_SetArc.CrossSize.ToString();
                Combox_CrossColor.Text = m_SetArc.CrossColor.ToString();
                Combox_CalibColor.Text = m_SetArc.CalibColor.ToString();
                Combox_LinesColor.Text = m_SetArc.ArcColor.ToString();
                this.propertyGrid1.SelectedObject = m_SetArc;
                this.propertyGrid1.Refresh();
                RefreshContour();
                tool_ResultInfo.Text = "刷新成功";
            }
        }

        private void checkBo_All_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBo_All.Checked)
            {
                checkBox_Location.Checked = true;
                checkBox_Lines.Checked = true;
                checkBox_Caliper.Checked = true;
                m_SetArc.DisplayLoction = true;
                m_SetArc.DisplayArc = true;
                m_SetArc.DisplayCalib = true;
            }
        }

        private void checkBox_Location_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Location.Checked)
            {
                m_SetArc.DisplayLoction = true;
            }
            else
            {
                m_SetArc.DisplayLoction = false;
                if (checkBo_All.Checked)
                {
                    checkBo_All.Checked = false;
                }
            }
        }

        private void checkBox_Caliper_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Caliper.Checked)
            {
                m_SetArc.DisplayCalib = true;
            }
            else
            {
                m_SetArc.DisplayCalib = false;
                if (checkBo_All.Checked)
                {
                    checkBo_All.Checked = false;
                }
            }
        }

        private void checkBox_Lines_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Lines.Checked)
            {
                m_SetArc.DisplayArc = true;
            }
            else
            {
                m_SetArc.DisplayArc = false;
                if (checkBo_All.Checked)
                {
                    checkBo_All.Checked = false;
                }
            }
        }

        private void btn_SaveParameter_Click(object sender, EventArgs e)
        {
            try
            {

                m_SetArc.LineWidth = Combox_LineWidth.Text.ToInt32();
                m_SetArc.CrossSize = Combox_CrossSize.Text.ToInt32();
                m_SetArc.CrossColor = Combox_CrossColor.Text;
                m_SetArc.CalibColor = Combox_CalibColor.Text;
                m_SetArc.ArcColor = Combox_LinesColor.Text;
                m_SetArc.WriteParameter();
                MessageBox.Show("参数保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空，请加载图像！";
                    MessageBox.Show("图像为空，请加载图像！");
                    return;
                }

                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                m_SetArc.LineWidth = Combox_LineWidth.Text.ToInt32();
                m_SetArc.CrossSize = Combox_CrossSize.Text.ToInt32();
                m_SetArc.CrossColor = Combox_CrossColor.Text;
                m_SetArc.CalibColor = Combox_CalibColor.Text;
                m_SetArc.ArcColor = Combox_LinesColor.Text;

                if (m_SetArc.Rows[1] > 0 && m_SetArc.Cols[1] > 0)
                {
                    m_SetArc.Fit_Arc(m_Image, hWindowControl1.HalconWindow, m_SetArc.Rows, m_SetArc.Cols);
                    //HOperatorSet.GenCrossContourXld(out HObject Center, m_SetArc.RowCenter, m_SetArc.ColCenter, 20, Math.PI / 4);
                    //HOperatorSet.DispObj(Center, hWindowControl1.HalconWindow);
                    if (m_SetArc.RowCenter.D > 0 && m_SetArc.ColCenter.D > 0)
                    {
                        this.tool_ResultInfo.Text = $"拟合圆成功  圆心X:{m_SetArc.ColCenter} Y:{m_SetArc.RowCenter} 半径R:{m_SetArc.RadiusCircle}";
                    }
                }
                else
                {
                    MessageBox.Show("绘制圆坐标为空,清先绘制圆");
                }
                //HTuple tuple = (HTuple)m_SetArc.Rows;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void 适应图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HTuple Width = new HTuple(), Height = new HTuple();
            HOperatorSet.GetImageSize(m_Image, out Width, out Height);
            MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
            m_Image.DispObj(hWindowControl1.HalconWindow);
        }

        private void 保存原图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveImage(m_Image);
        }

        private void 保存当前窗口图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveWindowDump(hWindowControl1.HalconWindow);
        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormArcHelp arcHelp = new FormArcHelp();
            arcHelp.ShowDialog();
        }
    }
}
